   //初始化数据
var pnum = "";   
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findDesignCellInfo",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data.list != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"design_cell_table_code": "设计单元编号"
		          			      	,"design_cell_table_name": "设计单元名称"
		          			      	,"project_design_phase_name":"设计阶段"
		          			      	,"project_design_content_name":"设计内容"
		          			      	,"project_design_major_name":"设计专业"
		          			      	,"orderBy":"排序规则"
									,"urule_path":"计算公式规则路径"
                                    ,"isShow":"是否显示"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"更新时间"
		          			      	,"operation":"操作"
		            			},
                                "stateData":[{"stateField":"isShow","stateArray":[{0:"否"},{1:"是"}]}],
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"updateDesignCellList","name":"编辑表单信息"},
                                    	{"event":"setUrule","name":"设置计算公式规则路径"},
                                    	{"event":"dependFC","name":"公式集常量集依赖"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}

	        },
	        error: function () {
	        }
	    });
   }
   
   //设置排序规则
   var sortRule = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
					      	"project_design_phase_id":"所属设计阶段",
					      	"project_design_major_id":"所属专业",
					      	"project_design_content_id":"所属设计内容"
					},"text":{
						    "project_design_phase_id":""
						    ,"project_design_major_id":""
							,"project_design_content_id":""
					},"input":{
						    "project_design_phase_id":"select"
						    ,"project_design_major_id":"new_select"
							,"project_design_content_id":"select"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
        layer.open({
           title:'设置排序规则'
           ,content: contentDiv()
           ,area: ['800px', '600px']
           ,scrollbar: false
           ,btn: ['确认', '取消']
           ,yes: function(index,layero){ //确认
               var data = $(layero).find("form").serializeJSON();
               if(!verify_data(data)){
                   return false;
               }
       	       var phase_id = data.project_design_phase_id;
       	       var major_id = data.project_design_major_id;
       	       var content_id = data.project_design_content_id;
    	       var url = base+"/dataCenter/businessdata/designCellSort/"+phase_id+"/"+major_id+"/"+content_id+"?name=设计公式排序";
    	       layer.close(index);
    	       $.get(url, function(result){
    			   $("#content").html("");
    			   $("#content").html(result);
    		   });
               return false;
           }
        });
		initPhase();
		initDesignMajor();
		chanageMajorselect();
   }
   

   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           design_cell_table_name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findDesignCellInfo",
           data: param,
           success: function (data, textStatus, jqXHR) {
               if(data.list != null){
                   var pageCount = data.pager.pageCount;
                   var dataObj ={
                       "list":data.list,
                       "dataTool":{
                           "title":{
                               "design_cell_table_code": "设计单元编号"
                               ,"design_cell_table_name": "设计单元名称"
                               ,"project_design_phase_name":"设计阶段"
                               ,"project_design_content_name":"设计内容"
                               ,"project_design_major_name":"设计专业"
                               ,"orderBy":"排序规则"
                               ,"urule_path":"计算公式规则路径"
                               ,"isShow":"是否显示"
                               ,"create_time":"创建时间"
                               ,"update_time":"更新时间"
                               ,"operation":"操作"
                           },
                           "stateData":[{"stateField":"isShow","stateArray":[{0:"否"},{1:"是"}]}],
                           "titleCheckbox":false,
                           "btn":[{"event":"updateObj","name":"编辑"},
                               {"event":"updateDesignCellList","name":"编辑表单信息"},
                               {"event":"delObj","name":"删除"}]
                       }
                   };
                   showPageTable('view', dataObj,num,pageCount,"searchObj");
               }

           },
           error: function () {
           }
       });
   }
   //删除设计单元
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var id = obj.id,code = obj.design_cell_table_code
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delDesignCellById/"+id+"/"+code,
			   data:{},
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   //添加设计单元资料
   var addObj = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "design_cell_table_code":"设计单元编号",
					      	"design_cell_table_name": "设计单元名称",
					      	"project_design_phase_id":"所属设计阶段",
					      	"project_design_major_id":"所属专业",
					      	"project_design_content_id":"所属设计内容",
                        	"isShow":"是否显示"
					},"text":{
						    "design_cell_table_code":""
						    ,"design_cell_table_name":""
							,"project_design_phase_id":""
							,"project_design_major_id":""	
							,"project_design_content_id":""
							,"isShow":""
					},"input":{
						    "design_cell_table_code":"text"
						    ,"design_cell_table_name":"text"
							,"project_design_phase_id":"select"
							,"project_design_major_id":"new_select"
							,"project_design_content_id":"select"
							,"isShow":"switch"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}

		layopen(contentDiv());
		initPhase();
		initDesignMajor();
		chanageMajorselect();
        initSwitch();
   }

   var initSwitch = function(flag){
       var html = '';
       debugger
       if(flag=="1"){
           html += '<input checked=""  name="isShow" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
       }else{
           html += '<input name="isShow" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
       }
       $("#switch").html(html);
       form.render();
   }

   //监听专业下拉选
   var chanageMajorselect = function(){
   		form.on("select(new_select)",function(value){
   			var major_id = value.value;
   			initContent(major_id);
   		});
   }
   
   var initContent = function(major_id){
 	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
 	   		  	function(data,status){
 	     		    	if(data!=''){
 	   	      		  	 var optionstring = "";
 	   	                 $.each(data.list, function(i,item){
 	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
 	   	                });
 	   	                $("select[name='project_design_content_id']").html('<option value=""></option>' + optionstring);
 	   	      		}else{
 	   	      		  	$("select[name='project_design_content_id']").html('<option value=""></option>');
 	   	      		}
 	     		    form.render(); 
 	  });
   }
    
    var initDesignMajor = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
    		  	function(data,status){
      		    	if(data!=''){
    	      		  	 var optionstring = "";
    	                 $.each(data.list, function(i,item){
    	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
    	                });
    	                $("select[name='project_design_major_id']").html('<option value=""></option>' + optionstring);
    	      		}else{
    	      		  	$("select[name='project_design_major_id']").html('<option value=""></option>');
    	      		}
      		    form.render(); 
   });
   }
    
    var initPhase = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
 	   		  	function(data,status){
     		    	if(data != null && data.list != null){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
   	                });
   	                $("select[name='project_design_phase_id']").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("select[name='project_design_phase_id']").html('<option value=""></option>');
   	      		}
     		    form.render(); 
 	  });
    }

    var dependFC = function(obj){
        var contentDiv = function(){
            var content;
            var data={
                "title":{
                    "id":"主键",
                    "design_cell_table_code":"设计单元编号",
                    "design_cell_table_name": "设计单元名称",
                    "project_design_phase_name":"所属设计阶段",
                    "project_design_major_name":"所属专业",
                    "project_design_content_name":"所属设计内容",
                    "formula_version_id":"公式集依赖",
                    "constant_version_id":"常量集依赖"
                },"text":{
                    "id":obj.id
                    ,"design_cell_table_code":obj.design_cell_table_code
                    ,"design_cell_table_name":obj.design_cell_table_name
                    ,"project_design_phase_name":obj.project_design_phase_name
                    ,"project_design_major_name":obj.project_design_major_name
                    ,"project_design_content_name":obj.project_design_content_name
                    ,"formula_version_id":""
                    ,"constant_version_id":""
                },"input":{
                    "id":"hidden"
                    ,"design_cell_table_code":"text_readonly"
                    ,"design_cell_table_name":"text_readonly"
                    ,"project_design_phase_name":"text_readonly"
                    ,"project_design_major_name":"text_readonly"
                    ,"project_design_content_name":"text_readonly"
                    ,"formula_version_id":"new1_checkbox"
                    ,"constant_version_id":"new_checkbox"
                },"input_init":{
                }
            };
            var getDiv =  dc_update.innerHTML;
            laytpl(getDiv).render(data, function(html){
                content = html;
            });
            return content;
        }
        layopen2(contentDiv());
        initConstants(obj);
        initFormulas(obj);
	}

   var initConstants = function(obj){
       $.post(base+"/dataCenter/businessdata/findConstantVersionList",{
               "design_cell_id":obj.id,
               "content_id":obj.project_design_content_id,
		   		"phase_id":obj.project_design_phase_id,
		   		"major_id":obj.project_design_major_id
		   },
           function(data,status){
               if(data!=''){
                   var checkboxString = "";
                   $.each(data, function(i,item){
                       if(item.isQy==true) {
                           checkboxString += " <input name = constant_v title = "+item.constant_table_name+" value="+item.id+" type='checkbox' checked>";
                       }else {
                           checkboxString += " <input name = constant_v title = "+item.constant_table_name+" value="+item.id+" type='checkbox'>";
                       }
                   });
                   $("#new_checkbox").html(checkboxString);
               }else{
                   $("#new_checkbox").html("无数据");
               }
               form.render();
           });
   }

   var initFormulas = function(obj){
       $.post(base+"/dataCenter/businessdata/findFormulaVersionList",{
                "design_cell_id":obj.id,
               "content_id":obj.project_design_content_id,
               "phase_id":obj.project_design_phase_id,
               "major_id":obj.project_design_major_id
           },
           function(data,status){
               if(data!=''){
                   var checkboxString = "";
                   $.each(data, function(i,item){
                       if(item.isQy==true) {
                           checkboxString += " <input name = formula_v title = "+item.formula_table_name+" value="+item.id+" type='checkbox' checked>";
                       }else {
                           checkboxString += " <input name = formula_v title = "+item.formula_table_name+" value="+item.id+" type='checkbox'>";
                       }
                   });
                   $("#new1_checkbox").html(checkboxString);
               }else{
                   $("#new1_checkbox").html("无数据");
               }
               form.render();
           });
   }

   //渲染弹窗
   var layopen2 = function(content){
       layer.open({
           title:'设置公式集常量集依赖'
           ,content: content
           ,area: ['800px', '600px']
       	   ,scrollbar: false
           ,btn: ['确认', '取消']
           ,yes: function(index,layero){ //确认
               var data = $(layero).find("form").serializeJSON();
               var formula_arr = new Array();
               $.each($('input[name=formula_v]:checked'),function(index,value){
                   formula_arr.push($(this).attr("value"));
               });
               var constant_arr = new Array();
               $.each($('input[name=constant_v]:checked'),function(index,value){
                   constant_arr.push($(this).attr("value"));
               });
               var formula_str = formula_arr.join(",");
               var constant_str = constant_arr.join(",");
               $.post(base+"/dataCenter/businessdata/updateDesignCellFormulaAndConstant",{
                       id:data.id==null?"":data.id,
                       constant_version_ids:constant_str,
                        formula_version_ids:formula_str
                   },
                   function (data,status){
                       if(data!=""&&data!=null){
                           if(data.statusCode == 0){
                               layer.msg('设置成功', {icon: 1,time:2000});
                           }else{
                               layer.msg(data.message, {icon: 2,time:2000});
                           }
                       }else{
                           layer.msg('设置失败!', {icon: 2,time:2000});
                       }
                       initData(pnum);
                   })
           }
       });
   }

   var setUrule = function(obj){
       var contentDiv = function(){
           var content;
           var data={
               "title":{
                   "id":"主键",
                   "design_cell_table_code":"设计单元编号",
                   "design_cell_table_name": "设计单元名称",
                   "project_design_phase_name":"所属设计阶段",
                   "project_design_major_name":"所属专业",
                   "project_design_content_name":"所属设计内容",
				   "urule_path":"公式规则路径"
               },"text":{
                   "id":obj.id
                   ,"design_cell_table_code":obj.design_cell_table_code
                   ,"design_cell_table_name":obj.design_cell_table_name
                   ,"project_design_phase_name":obj.project_design_phase_name
                   ,"project_design_major_name":obj.project_design_major_name
                   ,"project_design_content_name":obj.project_design_content_name
				   ,"urule_path":obj.urule_path
               },"input":{
                   "id":"hidden"
                   ,"design_cell_table_code":"text_readonly"
                   ,"design_cell_table_name":"text_readonly"
                   ,"project_design_phase_name":"text_readonly"
                   ,"project_design_major_name":"text_readonly"
                   ,"project_design_content_name":"text_readonly"
				   ,"urule_path":"text"
               },"input_init":{
               }
           };
           var getDiv =  dc_update.innerHTML;
           laytpl(getDiv).render(data, function(html){
               content = html;
           });
           return content;
       }
       layopen1(contentDiv());
   }

   //渲染弹窗
   var layopen1 = function(content){
       layer.open({
           title:'设置公式计算规则路径'
           ,content: content
           ,area: ['800px', '600px']
           ,scrollbar: false
           ,btn: ['确认', '取消']
           ,yes: function(index,layero){ //确认
               var data = $(layero).find("form").serializeJSON();
               if(!verify_data(data)){
                   return false;
               }
               $.post(base+"/dataCenter/businessdata/updateDesignCellUrulePath",{
                       id:data.id==null?"":data.id,
					   urule_path:data.urule_path
                   },
                   function (data,status){
                       if(data!=""&&data!=null){
                           if(data.statusCode == 0){
                               layer.msg('设置成功', {icon: 1,time:2000});
                           }else{
                               layer.msg(data.message, {icon: 2,time:2000});
                           }
                       }else{
                           layer.msg('设置失败!', {icon: 2,time:2000});
                       }
                       initData(pnum);
                   })
           }
       });
   }

   //编辑资料
   var updateObj = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"主键",
						    "design_cell_table_code":"设计单元编号",
					      	"design_cell_table_name": "设计单元名称",
					      	"project_design_phase_name":"所属设计阶段",
					      	"project_design_major_name":"所属专业",
					      	"project_design_content_name":"所属设计内容",
                            "isShow":"是否显示"
					},"text":{
						    "id":obj.id
						    ,"design_cell_table_code":obj.design_cell_table_code
						    ,"design_cell_table_name":obj.design_cell_table_name
							,"project_design_phase_name":obj.project_design_phase_name
							,"project_design_major_name":obj.project_design_major_name
							,"project_design_content_name":obj.project_design_content_name
                            , "isShow":""
					},"input":{
						    "id":"hidden"
						    ,"design_cell_table_code":"text"
						    ,"design_cell_table_name":"text"
							,"project_design_phase_name":"text_readonly"
							,"project_design_major_name":"text_readonly"
							,"project_design_content_name":"text_readonly"
                            ,"isShow":"switch"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
       initSwitch(obj.isShow);
   }
   
   //渲染弹窗
   var layopen = function(content){
		layer.open({
		  	title:'更新设计单元'
			,content: content
	    	,area: ['800px', '600px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
  		  	    var data = $(layero).find("form").serializeJSON();
  		  	    if(!verify_data(data)){
  		  	    	return false;
  		  	    }
                if(data.isShow == "on"){
                    data.isShow = true;
                }else{
                    data.isShow = false;
                }

				$.post(base+"/dataCenter/businessdata/updateDesignCellInfo",{
					id:data.id==null?"":data.id,
					design_cell_table_code:$.trim(data.design_cell_table_code),
					design_cell_table_name:$.trim(data.design_cell_table_name),
					project_design_phase_id:data.project_design_phase_id,
					project_design_major_id:data.project_design_major_id,
					project_design_content_id:data.project_design_content_id,
                    isShow:data.isShow
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }
   
   //编辑设计单元表单信息
   var updateDesignCellList = function(obj){
	   var code = "dc_designcell_"+obj.design_cell_table_code;
	   openStructurLay(code);
   }
   
   //打开结构化数据维护弹窗
   var openStructurLay = function(code){
		layer.open({
		  	title:'编辑表单'
			,content:initFormField(code)
	    	,area: ['1150px', '800px']
		    ,scrollbar: false
		  	,btn: ['保存', '取消']
			,yes: function(index,layero){ //确认
				commit_field_data(code);
			}
	    });
   }
   
   //校验表单数据
   var verify_table_data = function(){
	   var juage = true;
	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index>1){
			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var obj_name = $(this).children().attr("name");
					   var obj_val = $(this).children().val();
					   if("field_type"==obj_name && ("varchar"==obj_val || "int"==obj_val)){
						   var field_length_obj = $(this).parent().find("td input[name='field_length']");
						   if(field_length_obj.val() == "" || field_length_obj.val() == undefined){
							   field_length_obj.focus();
							   layer.tips('字段长度不能为空', field_length_obj);
							   juage = false;
							   return false;
						   }
					   }
					   if("field_name"==obj_name && (""==obj_val || undefined==obj_val)){
						   var field_name_obj = $(this).parent().find("td input[name='field_name']");
						   field_name_obj.focus();
						   layer.tips('字段名不能为空', field_name_obj);
						   juage = false;
						   return false;
					   }
				   }
			   });
			   if(!juage){
				   return false;
			   }
		   }
	   });
	   return juage;
   }
   
   
   //封装数据
   var commit_field_data = function(code){
	   if(false==verify_table_data()){
		   return false;
	   }
	   var json_array = [];
	   var data_obj = $("#from_field_table").children("tr");
	   $.each(data_obj,function(index,value){
		   if(index > 1){
			   var json_str = {};
			   $.each($(this).children("td"),function(children_index,value){
				   if(children_index > 1){
					   var input_obj = $(this).children();
					   var input_name = $(this).children().attr("name");
					   if(input_name=="field_name"){
						   json_str['field_name']=$.trim(input_obj.val());
					   }else if(input_name=="field_comment"){
						   json_str['field_comment']=$.trim(input_obj.val());
					   }else if(input_name=="field_length"){
						   json_str['field_length']=$.trim(input_obj.val());
					   }else if(input_name=="field_default"){
						   json_str['field_default']=$.trim(input_obj.val());
					   }else if(input_name=="field_type"){
						   json_str['field_type']=$.trim(input_obj.val());
					   }else if(input_name=="is_null"){
						   if(input_obj.is(':checked')){
							   json_str['is_null']='true';
						   }else{
							   json_str['is_null']='false';
						   }
					   }else if(input_name=="widget_type"){
						   json_str['widget_type']=$.trim(input_obj.val());
					   }else if(input_name=="form_default"){
						   json_str['form_default']=$.trim(input_obj.val());
					   }else if(input_name=="is_show"){
						   if(input_obj.is(':checked')){
							   json_str['is_show']='true';
						   }else{
							   json_str['is_show']='false';
						   }
					   }else if(input_name=="widget_length"){
						   json_str['widget_length']=$.trim(input_obj.val());
					   }else if(input_name=="form_type"){
						   json_str['form_type']=$.trim(input_obj.val());
					   }else if(input_name=="field_order"){
						   json_str['field_order']=$.trim(input_obj.val());
					   }
				   }
			   });
			   json_array.push(json_str);
		   }
	   });
	   var field_list = JSON.stringify(json_array);
	   var table_name = code;
	   var param = {"table_name":table_name,"data_list":field_list};
	   commit_data(param);
   } 
   
   var commit_data = function(param){
	   $.post(base+"/dataCenter/businessdata/updateTableField",param,
		       function(data,status){
			     if(data.statusCode == 0){
				  	layer.msg(data.message, {
						icon : 1
				  	});
			     }else{
					layer.msg(data.message, {
						icon : 5
					});
			    }
		  });
   }
   
   
   //取得表单信息
   var initFormField = function(code){
	   var contentDiv;
	   $.ajax({
		   url:base+"/dataCenter/findFromFieldHtml/"+code,
		   data:{},
		   async: false,
		   success:function(data,status){
			   contentDiv = data;
		   }
	   });
	   return contentDiv;
   }
   
   //校验数据
   var verify_data = function(data){
		if(data.design_cell_table_code == ""){
			var obj = $("input[name='design_cell_table_code']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.design_cell_table_name == ""){
			var obj = $("input[name='design_cell_table_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.project_design_phase_id == ""){
			alert("请选择设计阶段!");
			return false;
		}
		if(data.project_design_major_id == ""){
			alert("请选择设计专业");
			return false;
		}
		if(data.project_design_content_id == ""){
			alert("请选择设计内容!");
			return false
		}
		return true;
   }
   
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addObj);
       $("#searchInfo").click(searchObj);
       $("#sortRule").click(sortRule);
   });
   